<template>
<div>
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
    <el-breadcrumb-item :to="{ path: '/' }">
      <i class="el-icon-s-promotion"></i>后台管理
    </el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: '/news/newsDetail' }">新闻管理</el-breadcrumb-item>
    <el-breadcrumb-item><a href="/">添加新闻</a></el-breadcrumb-item>
  </el-breadcrumb>
  <el-divider></el-divider>
  <div>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <div style="width: 60%;">
    <el-form-item label="新闻题目" prop="title">
      <el-input v-model="ruleForm.title"></el-input>
    </el-form-item>
    </div>
    <div style="width: 30%;">
    <el-form-item label="作者" prop="author">
      <el-input v-model="ruleForm.author"></el-input>
    </el-form-item>
    </div>
    <el-form-item label="资讯分类" prop="newsCategoryId">
      <el-select v-model="ruleForm.newsCategoryId" placeholder="请选择资讯分类" style="width: 300px;">
        <el-option
            v-for="item in newsCategoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="正文" prop="content">
<!--      <el-input v-model="ruleForm.content"></el-input>-->
      <div id="wangEditor"></div>
    </el-form-item>



    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
      <el-button type="warning" @click="$router.push('/news/newsDetail')">返回列表</el-button>
    </el-form-item>
  </el-form>
  </div>
</div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        id:'',
        title: '',
        author:'',
        newsCategoryId:'',
        content:'',

      },
      rules: {
        title: [
          {required: true, message: '请输入标题', trigger: 'blur'},
          {min: 4, max: 25, message: '长度在 4 到 25 个字符', trigger: 'blur'}
        ],
        author: [
          {required: true, message: '请输入作者', trigger: 'blur'},
          {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
        ],
        newsCategoryId: [
          {required: true, message: '请选择资讯分类', trigger: 'blur'},
        ],
      },
      newsCategoryList: [],
      // 富文本编辑器
      editor: {}
    };
  },
  methods: {
    // 初始化富文本编辑器
    initWangEditor() {
      this.editor = new this.wangEditor('#wangEditor');
      this.editor.config.zIndex = 1;
      this.editor.create();
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9200/newsDetail/add-new';
          this.ruleForm.content = this.editor.txt.html();

          // let  formData = 'name=' + this.ruleForm.name
          //       + '&description=' + this.ruleForm.description
          //       + '&sort=' + this.ruleForm.sort;

          let formData = this.qs.stringify(this.ruleForm);
          console.log(this.ruleForm,'1111111111')
          console.log("formData=" + formData)

          this.axios.post(url,formData).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state==20000){
              this.$message({
                message: '添加新闻成功',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }

          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadNewsCategoryList() {
      let url = 'http://localhost:9200/news-category/list';
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.newsCategoryList = jsonResult.data;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    }
  },
  mounted(){
    this.loadNewsCategoryList();
    this.initWangEditor();
  }
}
</script>

<style scoped>

</style>